﻿using Microsoft.Extensions.Configuration;
using System.Collections;

namespace AME.Setting;

public static class SettingExtensions
{
    /// <summary>
    /// 环境变量
    /// EnvironmentVariableTarget 默认全选(Process;User;Machine)
    /// </summary>
    public static IDictionary Env => Environment.GetEnvironmentVariables();

    /// <summary>
    /// 环境名称
    /// </summary>
    public static string EnvironmentName => Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";

    /// <summary>
    /// 用设置和环境变量
    /// </summary>
    public static IConfigurationRoot Configuration => GetAppSettingAndEnvironment();

    /// <summary>
    /// 为基础层提供应用设置和环境变量
    /// </summary>
    public static IConfigurationRoot GetAppSettingAndEnvironment()
    {
        // Build a config object, using env vars and JSON providers.
        IConfigurationRoot config = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{EnvironmentName}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();

        return config;
    }
}
